<?php
/** 命名空间 */
namespace app\hy;
/**
 * 出错提示错
 */
function error($error)
{
	echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
	'<script type="text/javascript">',
	'alert(' . json_encode($error) . ');',
	'</script>';
	// 从官网新开窗口:本页自动关闭.
	echo '<script type="text/javascript">window.opener = null; window.open("", "_self", ""); window.close(); </script>';
	exit();
}


/**
 * 冻结IP用户
 */
function control_ip(\ounun\Mysql $db,$ip,$time) 
{
	$ip_control = $db->row("select * from `gchy_control_ip` where `ip`= ? ",$ip);
	if($ip_control)
	{
		if($ip_control['end_time'] > $time){
			$error  = '帐号已冻结,无法进入游戏(error:control_ip)';
			return array(false,$error);
		}else{
			$db->delete("`gchy_control_ip`",' `ip`= ? ',$ip);
		}
	}
	return array(true);
}

/**
 * UID转Cid与 account
 */
function core_uid2account(\ounun\Mysql $db,$uid)
{
    return $db->row("select `cid`,`account` from `hyapi_user` where `uid` = ? ",$uid);
}

/**
 * Cid与 Uname转UID
 */
function core_account2uid(\ounun\Mysql $db,$cid,$account)
{
    $bind = array(
            'cid'		=> $cid,
            'account' 	=> $account
    );
    $rs = $db->row("select `uid` from `hyapi_user` where `cid`=:cid and `account`=:account ",$bind);
    // echo $db->getSql();
    if($rs){
        return (int)$rs['uid'];
    }
    return 0;
}
/**
 * 联运用户激活
 * @param $core_db     Mysql
 * @param $cid 			unit  	联运商cid
 * @param $account 		string 	用户帐号
 * @return $uid   		unit	用户ID
 */
function core_activate_union(\ounun\Mysql $core_db,$cid,$account) 
{
	$rs   = $core_db->row("select `uid` from `hyapi_user` where `account` = :account and `cid` =:cid ;",array('cid'=>$cid,'account'=>$account) );
	if($rs && $rs['uid'])
	{
		return array(true,$rs['uid']);
	}else
	{
		$bind = array('cid' 		=> $cid,
					   'account'   	=> $account,
					   'time'		=> time()    );
		$uid  = $core_db->insert("`hyapi_user`",$bind);
		if($uid)
		{
			return array(true, $uid);
		}else
		{
			return array(false,"insert");
		}
	}
}
/**
 * QQ用户激活
 * @param $pf     String	来源
 * @param $openid String  	OpenID
 * @return Uid   	Uint	用户ID
 */
function core_activate_qq(\ounun\Mysql $core_db,$openid,$pf)
{
	$rs   = $core_db->row("select `uid` from `hyapi_user_qq` where `openid` = ? ;",$openid);
	if($rs && $rs['uid'])
	{
		return array(true,$rs['uid']);
	}else
	{
		$bind = array('openid' => $openid,
					   'time'   => time(),
					   'pf'		=> $pf);
		$uid  = $core_db->insert("`hyapi_user_qq`",$bind);
		if($uid)
		{
			return array(true, $uid);
		}else 
		{
			return array(false,"insert");
		}		
	}
}
/**
 * 创建玩家基本数据
 */
function user_create(\ounun\Mysql $db,$uid,$cid,$account,$time,$ip)
{	
	$bind = array(
			'uid'	 	=> $uid,    
			'source'	=> '',   
			'cid'	 	=> $cid,  
			'account'	=> $account,
			'uname'	 	=> '',
			'pro'	 	=> 3,
			'sex'	 	=> 0,
			'lv'	 	=> 0,
			'gold'	 	=> 0,
			'rmb'	 	=> 0,
			'point'	 	=> 0,
			'rmb_sum' 	  => 0,
			'rmb_consume' => 0,
			'vip'	  	  => 0,
			'vip_indate'	=> 0,
			'reg_time'		=> $time,
			'reg_ip'		=> $ip,
			'login_times'	=> 1,
			'state'			=> 0,
			'login_last'	=> $time,
			'login_ip'		=> $ip,
			'online'		=> 0
		);
	$db->insert("`gchy_user`",$bind);
	if (Const_Serv_Create_Enable)
	{
	    $rmb	= Const_Serv_Create_RMB;
	    $point	= Const_Serv_Create_Point;
	    if($rmb > 0 || $point > 0)
	    {
	        \app\hy\user_pay($db, Const_Serv_Sid, $uid,'sys','create',  '',$rmb,$point,'',false);
	    }
	}
}
/**
 * 记录来源
 */
function user_zoom(\ounun\Mysql $db,$uid,$ip)
{
	$time	 = time();
	$ip_obj  = new \plugins\qqwry\QQWry('utf-8');
	$iplist  = $ip_obj->getlocation($ip);
	// gchy_user_zoom
	$zoom = array(
			'uid'			=> $uid,
			'progress'		=> 'create',
			'ip'			=> $ip,
			'area'			=> $iplist['country'],
			'address'		=> $iplist['area'],
			'screen'		=> $_REQUEST['screen'],
			'language'		=> $_SERVER['HTTP_ACCEPT_LANGUAGE'],
			'UserAgent'		=> $_SERVER['HTTP_USER_AGENT'],
			'reference'		=> $_REQUEST['referrer'],
			'YmdH'			=> date('YmdH'),
			'time'			=> $time,
	);
	$db->insert("`gchy_user_zoom`",$zoom);
}
/**
 * 记录登录日志
 */
function user_login_logs(\ounun\Mysql $db,$uid,$cid,$account,$uname,$time, $ip)
{
	$logs	= array( 'uid'		=> $uid,
					  'cid'		=> $cid,
			          'account'	=> $account,
			          'uname'	=> $uname,
			          'ip'		=> $ip,
			          'time'	=> $time);
	$db->insert("`gchy_logs_login`",$logs);
}

/**
 * 更新用户登录数据
 */
function user_data_update(\ounun\Mysql $db,$uid,$login_times,$login_last,$time,$ip)
{
	$update = array(
			'login_times'	=> $login_times,
			'login_last'	=> $time,
			'login_ip'		=> $ip,
	);
	if(date('Ymd',$time) != date('Ymd',$login_last))
	{
		if(Const_Serv_Login_Enable)
		{
		    $rmb 	= Const_Serv_Login_RMB;
		    $point	= Const_Serv_Login_Point;		    
			if (Const_Serv_Login_Reset)
			{
				$update['rmb']	 	= $rmb;
		        $update['point']	= $point;
			}else {
			    \app\hy\user_pay($db, Const_Serv_Sid, $uid,'sys','login',  '',$rmb,$point,'',false);
			}
		}
	}
	$db->update("`gchy_user`",$update,' `uid` = ? ',$uid);
}


/**
 * UID转Cid与 account
 */
function user_uid2account(\ounun\Mysql $db,$uid)
{
	return $db->row("select `cid`,`account` from `gchy_user` where `uid` = ? ",$uid);
}

/**
 * Cid与 Uname转UID
 */
function user_account2uid(\ounun\Mysql $db,$cid,$account)
{
	$bind = array(
			'cid'		=> $cid,
			'account' 	=> $account
	);
	$rs = $db->row("select `uid` from `gchy_user` where `cid`=:cid and `account`=:account ",$bind);
	// echo $db->getSql();
	if($rs){
		return (int)$rs['uid'];
	}
	return 0;
}
/**
 * 得到Uid
 * @param \ounun\Mysql $db
 * @param uint $cid
 * @param string $info_field
 * @param string|int $info_value
 */
function user_info2uid(\ounun\Mysql $db,$cid,$info_field,$info_value)
{
    return user_info_private($db, $cid, 'uid', $info_field, $info_value);
}
/**
 * 得到Uname
 * @param \ounun\Mysql $db
 * @param uint $cid
 * @param string $info_field
 * @param string|int $info_value
 */
function user_info2uname(\ounun\Mysql $db,$cid,$info_field,$info_value)
{
    return user_info_private($db, $cid, 'uname', $info_field, $info_value);
}
/**
 * 得到account
 * @param \ounun\Mysql $db
 * @param uint $cid
 * @param string $info_field
 * @param string|int $info_value
 */
function user_info2account(\ounun\Mysql $db,$cid,$info_field,$info_value) 
{
    return user_info_private($db, $cid, 'account', $info_field, $info_value);
}
/**
 * @property
 * 各种得到
 * @param \ounun\Mysql $db
 * @param uint $cid
 * @param string $get_field
 * @param string $info_field
 * @param string|int $info_value
 */
function user_info_private(\ounun\Mysql $db,$cid,$get_field,$info_field,$info_value)
{
    $info_field   = in_array($info_field, array('uname','uid','account'))?$info_field:'uid';
    $get_field    = in_array($get_field , array('uname','uid','account'))?$get_field :'uid';
    if($info_field == $get_field)
    {
        return $info_value;
    }else
    {
        $rs       = $db->row("select `{$get_field}` from `gchy_user` where `{$info_field}` = :info_value and `cid` = :cid limit 1;",array('info_value'=>$info_value,'cid'=>$cid) );
        if($rs)
        {
            return $rs[$get_field];
        }
        return null;
    }
}
/**
 * 充值接口
 *    充值: type     <- $type <- 'pay'
 *          type_sub <- $pid
 *          item     <- $oid
 *          remark   <- '充值'
 *          
 *    GM :  type     <- $type <- 'gm'
 *          type_sub <- $pid  <- 'inside' 研发内部操作 | 'gm' 研发GM命令 | 'operation' 运营
 *          item     <- $oid  <- $account 管理员帐号;
 *          remark   <- $remark
 *    
 *    SYS   type     <- $type <- 'sys'
 *          type_sub <- $pid  <- 'create' 创建 | 'login' 创建
 *          item     <- $oid  <- ''
 *          remark   <- $remark  <- ''
 *          
 *    研发充值: user_pay($db,$sid,$uid,'gm','inside',   $account,$rmb,$point,$remark)
 *    平台充值: user_pay($db,$sid,$uid,'gm','operation',$account,$rmb,$point,$remark)
 *    系统充值: user_pay($db,$sid,$uid,'sys','create',  '',$rmb,$point,'')
 *          
 * @param \ounun\Mysql $db
 * @param uid $uid
 * @param uid $pid       日志:type_sub
 * @param uid $oid       日志:item
 * @param uid $rmb
 * @param uid $point     
 * @param uid $remark    日志:充值
 */
function user_pay(\ounun\Mysql $db,$sid,$uid,$type,$pid,$oid,$rmb,$point,$remark,$call_serv=true)
{
    // 查出余额
    $rs    = $db->row('SELECT `rmb`,`point`,`rmb_sum` FROM  `gchy_user` where `uid` = ? ',$uid);
    if(!$rs)
    {
        return array(false,201,'帐号当前服未激活或帐号不存在');
    }
    // 充值
    if ('pay' == $type)
    {
        $rs_logs   = $db->row('SELECT `uid` FROM  `gchy_logs_rmb` where `type_sub` = :pid or `item` = :oid limit 1;',array('pid'=>$pid,'oid'=>$oid));
        if($rs_logs)
        { // 充值 查是否重复充值
            \app\erl\give_pay($sid, $uid);
            return array(true,$rs['rmb'],$rs['point'],$rs['rmb_sum']);
        }
        // 把金额 往帐户上 添加
        $rs_update    = $db->add('`gchy_user`', array('rmb'=>$rmb,'point'=>$point,'rmb_sum'=>$rmb), ' `uid` = ? ',$uid);
    }else 
    {
        // 把金额 往帐户上 添加
        $rs_update    = $db->add('`gchy_user`', array('rmb'=>$rmb,'point'=>$point), ' `uid` = ? ',$uid);
    }
    // 日志
    if($rs_update)
    {
        $bind = array(
                    'time'     => time(),
                    'uid'      => $uid,
                    'type'     => $type,
                    'type_sub' => $pid,
                    'item'     => $oid,
                    'amount'   => 0,
                    'balance'  => 0,
                    'remark'   => $remark
                );
        if($rmb > 0)
        {
            $bind['amount']    = $rmb;
            $bind['balance']   = $rmb+$rs['rmb'];
            $db->insert('`gchy_logs_rmb`', $bind);
        }
        if($point > 0)
        {
            $bind['amount']    = $point;
            $bind['balance']   = $point+$rs['point'];
            $db->insert('`gchy_logs_point`', $bind);
        }
        if($call_serv)
        {
            \app\erl\give_pay($sid, $uid);
        }        
        // 返回
        return array(true,$rs['rmb']+$rmb,$rs['point']+$point,$rs['rmb_sum']+$rmb);
    }else 
    {
        return array(false,303,'数据库更新失败');
    }    
}

/**
 * 变量 替换
 */
function var_replace($var,$sid,$csid,$area,$para,$account)
{
	if($sid)
	{
		if($para)
		{
			$para 	= str_replace('{sid}', $sid, $para);
		}
		$var 		= str_replace('{sid}', $sid, $var);
	}
	if($csid)
	{
		if($para)
		{
			$para 	= str_replace('{csid}', $csid, $para);
		}
		$var 		= str_replace('{csid}', $csid, $var);
	}
	if($area)
	{
		if($para)
		{
			$para 	= str_replace('{area}', $area, $para);
		}
		$var 		= str_replace('{area}', $area, $var);
	}
	if($account)
	{
		if($para)
		{
			$para 	= str_replace('{account}', $account, $para);
		}
		$var 		= str_replace('{account}', $account, $var);
	}
	if($para)
	{
		$var 		= str_replace('{para}', $para, $var);
	}
	return $var;
}



/**
 * 记录发放日志
 * @param \ounun\Mysql $db
 * @param  $cid
 * @param  $uid
 * @param  $uname
 * @param  $type
 * @param  $content
 * @param  $account
 * @param  $ip
 * @param  $address
 * @param  $time
 */
function provide(\ounun\Mysql $db,$cid,$uid,$uname,$type,$content,$account)
{
    $time   = time();
    $ip	    = \ounun\ip();
    $wry    = new \plugins\qqwry\QQWry('utf-8');
    $uCity  = $wry->getlocation($ip);
    $address= $uCity["country"];
    $date   = array(
            'cid'     => $cid,
            'uid'     => $uid,
            'uname'   => $uname,
            'type'    => $type,
            'content' => serialize($content),
            'account' => $account,
            'ip'      => $ip,
            'address' => $address,
            'time'    => $time,
    );
    $db->insert("`gchy_cp_logs`",$date);
}

/**
 * 检查物品过期时间
 * @param  $time_type
 * @param  $value
 */
function checktime($time_type,$value){
	if(0 == $time_type){
		$time = 0;
	}
	if(1 == $time_type){
		$time = $value;
	}
	if(2 == $time_type){
		$time = $value*86400;
	}
	return $time;
}